package Exam;

import java.io.*;
import java.time.LocalDate;
import java.util.List;

public class FilePressImpl implements IFilePress{
    @Override
    public void read(List<Student> list) {
        Reader r = null;
        BufferedReader br = null;
        try {
            r = new FileReader("Test9_18/student.txt");
            br = new BufferedReader(r);
            String str = null;
            while ((str=br.readLine())!=null){
                String[] value = str.split("\\s+");
                Student student = new Student(Integer.parseInt(value[0]),value[1],value[2], LocalDate.parse(value[3]));
                list.add(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                br.close();
                r.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void write(List<Student> list) {
        Writer w = null;
        for (Student s:list){
            try {
                w = new FileWriter("Test9_18/"+s.getClasses()+".txt",true);
                w.write(s.getCode()+"  "+s.getName()+"  "+s.getClasses()+"  "+s.getBirthday()+"\n");
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    w.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    try {
                        w.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
